package com.jk.mail;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * 邮箱发送
 *
 * @author 高紫腾
 * @create 2018-01-08 13:55
 **/
@Controller
@RequestMapping("mail")
public class SimpleEmailClient {

    @RequestMapping("SimpleEmail")
    @ResponseBody
    public Map SimpleEmail(String mail) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");//必须 普通客户端
        props.setProperty("mail.transport.protocol", "smtp");//必须选择协议
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);//设置debug模式   在控制台看到交互信息
        Message msg = new MimeMessage(session);  //建立一个要发送的信息

        String str = "";

        msg.setText("http://www.baidu.com");//设置简单的发送内容
        msg.setFrom(new InternetAddress("gaozt_0421@163.com"));//发件人邮箱号
        Transport transport = session.getTransport();//发送信息的工具
        transport.connect("smtp.163.com", 25, "gaozt_0421@163.com", "gzt12345");//发件人邮箱号 和密码
        transport.sendMessage(msg, new Address[] { new InternetAddress(mail) });//对方的地址

        transport.close();

        Map<String,String> map = new HashMap<String, String>();

        map.put("mas","发送成功");
        return map;
    }

}
